package com.hyh.music.config;

import com.hyh.music.interceptor.JwtTokenAdminInterceptor;
import com.hyh.music.json.JacksonObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;


import java.util.List;

/**
 * 配置类，注册web层相关组件
 */
@Configuration
@Slf4j
public class WebMvcConfiguration extends WebMvcConfigurationSupport {

    @Autowired
    private JwtTokenAdminInterceptor jwtTokenAdminInterceptor;

    /**
     * 注册自定义拦截器
     *
     * @param registry
     */
    protected void addInterceptors(InterceptorRegistry registry) {
        log.info("开始注册自定义拦截器...");
        registry.addInterceptor(jwtTokenAdminInterceptor)
                .addPathPatterns("/admin/**")
                .addPathPatterns("/singer")
                .addPathPatterns("/song")
                .excludePathPatterns("/admin/login")
                .excludePathPatterns("/admin/common/*");
    }
    /**
     * 静态资源加载设置
     * @param registry
     */
    static final String MUSIC_PATH = System.getProperty("user.dir") + "/Song/";

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        log.info("静态资源配置");
        registry.addResourceHandler("/Song/**")
                .addResourceLocations("file:" +MUSIC_PATH );
        super.addResourceHandlers(registry);
    }

//    @Override
//    public void addResourceHandlers(ResourceHandlerRegistry registry){
//        registry.addResourceHandler("/music/**").addResourceLocations(
//                "fire:" + System.getProperty("user.dir") + System.getProperty("file.separator")
//                        + "music" + System.getProperty("file.separator")
//        );
//    }



//    /**
//     * 扩展springMVC框架的消息转换器
//     * 创建一个消息转换器对象，并对消息转换器进行对象赋值，并将创建的消息转换器加到框架中的消息转换器集合中
//     * @param converters
////     */
//    @Override
//    protected void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
//        log.info("扩展消息转换器对日期进行处理");
//        MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
//        converter.setObjectMapper(new JacksonObjectMapper());
//        converters.add(0,converter);
//    }





}
